package eerbaar.v2.app.core;

import java.awt.Component;
import java.awt.Dimension;

import javax.swing.JPanel;

public class RightPanel implements IGamePanelListener {

    private final JPanel panel = new JPanel();

    public RightPanel() {
        this.panel.setLayout(null);
    }

    public void addGamePanel(GamePanel gamePanel) {
        this.panel.add(gamePanel.getPanel());
        positionPanels();
    }

    public JPanel getPanel() {
        return this.panel;
    }

    @Override
    public void onHeightChanged() {
        this.positionPanels();
    }

    private int positionPanel(int y, Component gamePanel) {
        Dimension size = gamePanel.getPreferredSize();
        gamePanel.setBounds(5, y, size.width, size.height);
        y += size.height;
        y += 5;
        this.panel.setPreferredSize(new Dimension(300, size.height));
        return y;
    }

    private void positionPanels() {
        int y = 10;
        Component[] components = this.panel.getComponents();
        for (Component component : components) {
            y = positionPanel(y, component);
        }
    }

}
